﻿@model Opserver.Views.Home.AboutModel
@inject PollingService Poller
@{
    this.SetPageTitle("About - Caches");
    var pollingInfo = Poller.GetPollingStatus();
    var ts = Poller.GetThreadStats();
    var pollerNodes = pollingInfo.Nodes
        .OrderByWorst(n => n.DataPollers.GetWorstStatus())
        .ThenByDescending(n => n.IsPolling)
        .ThenBy(n => n.GetType().Name)
        .ThenBy(n => n.UniqueKey).ToList();
    if (Model.Filter.HasValue())
    {
        pollerNodes = pollerNodes.Where(p => p.UniqueKey.IndexOf(Model.Filter, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();
    }
}
@section head {
@if (Model.AutoRefresh)
{
    <script>Status.Dashboard.init({ refresh: 10 });</script>
}
}
<h5 class="page-header">
    Pollers
    <span class="text-muted small">(@pollingInfo.NodeCount.Pluralize("node"), @pollingInfo.TotalPollers.Pluralize("poller"), @pollingInfo.ActivePolls.ToComma() active)</span>
    <span class="pull-right">
        <a href="?refresh=@(Model.AutoRefresh ? "false" : "true")">@(Model.AutoRefresh ? "Disable" : "Enable") Auto Refresh</a>
        <a href="#" class="js-reload-link">Reload</a>
    </span>
</h5>
<div class="row">
    <div class="col-xs-12">
        <div class="panel panel-default small">
            <div class="panel-heading">Thread Pool</div>
            <div class="panel-body js-refresh" data-name="threadpool-stats">
                <div>
                    <label>IO Completion</label>:
                    <span class="text-muted">Busy:</span> @ts.BusyIOThreads.ToComma()<span class="text-muted">, Free:</span> @ts.AvailableIOThreads.ToComma()<span class="text-muted">, Min:</span> @ts.MinIOThreads.ToComma()<span class="text-muted">, Max:</span> @ts.MaxIOThreads.ToComma()
                </div>
                <div>
                    <label>Worker</label>:
                    <span class="text-muted">Busy:</span> @ts.BusyWorkerThreads.ToComma()<span class="text-muted">, Free:</span> @ts.AvailableWorkerThreads.ToComma()<span class="text-muted">, Min:</span> @ts.MinWorkerThreads.ToComma()<span class="text-muted">, Max:</span> @ts.MaxWorkerThreads.ToComma()
                </div>
            </div>
        </div>
    </div>
</div>

<table class="table table-hover table-responsive text-nowrap table-super-condensed">
    <thead>
        <tr>
            <th>Type</th>
            <th>Name</th>
            <th>Summary</th>
            <th>Last</th>
            <th>Next</th>
            <th>Thread</th>
            <th>Action</th>
        </tr>
    </thead>
    @foreach (var pn in pollerNodes)
    {
        var pnType = pn.GetType();
        var pollStatus = pn.PollStatus;
        <tbody class="js-refresh" data-name="Node-@pn.NodeType-@pn.UniqueKey">
            <tr class="cache-item active @pn.RowClass()">
                <th title="@pnType.FullName">@pn.IconSpan() @pnType.Name</th>
                <th>@pn.UniqueKey</th>
                <th>@pn</th>
                <th>@(pn.LastPoll?.ToRelativeTimeSpanMini()) <span class="text-muted small">(@pn.LastPollDuration.ToTimeStringMini())</span></th>
                <th></th>
                <th>
                    @if (pn.IsPolling)
                    {
                        <strong class="text-success">@pollStatus</strong>
                    }
                    else
                    {
                        <span class="text-muted">@pollStatus</span>
                    }
                </th>
                <th class="actions">
                    <a href="~/json/@pn.NodeType/@pn.UniqueKey">JSON</a>
                    <a href="#" class="js-poll-now" data-type="@pn.NodeType" data-key="@pn.UniqueKey">Poll All</a>
                </th>
            </tr>
            @foreach (var p in pn.DataPollers.OrderByWorst().ThenByDescending(p => p.IsPolling).ThenBy(p => p.ParentMemberName))
            {
                <tr class="cache-sub-item @p.RowClass()@(p.IsPolling ? " success" : null)">
                    <td title="Type: @pn.NodeType
Key: @pn.UniqueKey
Property: @p.ParentMemberName
Url: /@pn.NodeType/@pn.UniqueKey.UrlEncode()/@p.ParentMemberName">
                        <span title="@p.ErrorMessage">@p.IconSpan()</span> @p.Type.ReadableTypeDescription()
                    </td>
                    <td title="@p.ParentMemberName
@p.SourceFilePath @("@") line @p.SourceLineNumber.ToString()">@p.ParentMemberName</td>
                    <td>
                        @(p.InventoryDescription ?? "(empty)")
                        <small>
                            @{
                                if (!p.ContainsData) {<span class="text-danger"> (No Data)</span>}
                                else if (p.IsStale){<span class="text-warning"> (Stale)</span>}
                            }
                            (@p.PollsTotal.Pluralize("poll"): @p.PollsSuccessful.ToComma() successful)
                        </small>
                    </td>
                    <td>
                        @if (p.LastPoll == null)
                        {
                            <span class="text-danger small">Never</span>
                        }
                        else
                        {
                            @:@p.ToPollSpan() <span class="text-muted small">(@(p.LastPollDuration?.ToTimeStringMini()))</span>
                        }
                    </td>
                    <td class="text-muted small">@(p.NextPoll?.ToRelativeTimeSpanMini())</td>
                    <td class="@(p.IsPolling ? "text-success small" : "text-muted small")">(@(p.PollStatus.IsNullOrEmptyReturn("none")))</td>
                    <td class="actions">
                        <a href="~/json/@pn.NodeType/@pn.UniqueKey.UrlEncode()/@p.ParentMemberName.UrlEncode()">JSON</a>
                        @if (p.Profiler != null)
                        {
                            <a href="~/profiler/results?id=@(p.Profiler?.Id.ToString())" target="_blank">Profile</a>
                        }
                        else
                        {
                            <span class="text-muted">Profile</span>
                        }
                        <a href="#" class="js-poll-now" data-type="@pn.NodeType" data-key="@pn.UniqueKey" data-id="@p.UniqueId.ToString()">Poll</a>
                    </td>
                </tr>
            }
        </tbody>
    }
</table>